<?php
require_once ("../src/db/connectdatabase.php");

function getCardLevels() {
	$con = new ConnectDB();
	
	$tbl = "cardlevel";
	$fn = array('idcardlevel', 'cardlevelname', 'datecreate');
	$condetion = array('isuse' => true);
	return $con -> getvalueorderby($tbl, $fn, $condetion, 'datecreate');
}

function getCardLevel($id) {
	$con = new ConnectDB();
	
	$tbl = "cardlevel";
	$fn = array('idcardlevel', 'cardlevelname', 'datecreate');
	$condetion = array('idcardlevel' => $id);
	return $con -> getvalue($tbl, $fn, $condetion);
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}

$arrcardtype = getCardLevels();

?>
<style>
.listbanksviewcontent table {
	width: 850px;
	max-width: 850px;
}

.listbanksviewcontent table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

.listbanksviewcontent table td img {
	cursor: pointer;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.stylenamebank {
	padding-left: 5px;
	padding-right: 5px;
	width: 350px;
	max-width: 350px;
}

.styleacctionbank {
	width: 150px;
	text-align: center;
	max-width: 150px;
}

.styledatecreatebank {
	width: 100px;
	max-width: 100px;
	text-align: center;
}

.nameformbank {
	background: url(../images/bg-login.png);
	-moz-border-radius: 2px 2px 2px 2px;
	-webkit-border-radius: 2px 2px 2px 2px;
	border-radius: 2px 2px 2px 2px;
}

.center {
	text-align: center;
}

.listbanksviewcontent table tr:hover td {
	background: #FFC;
}

.stylestt {
	width: 70px;
}

</style>
<h3>Quản Lý Danh Sách Card Level</h3>
<div style="margin:auto; width:500px;">
<div style="color: red;">
<?php
	if(isset($_GET['mess'])) {
		echo($_GET['mess']);
	}
?>
</div>
<?php
if(isset($_GET['idct'])) {
	$idcardlevel = $_GET['idct'];
	$arrclevel = getCardLevel($idcardlevel);
	if(count($arrclevel) == 1) {
	?>
<form action="../src/control/admincardlevel.php" method="post" onSubmit="return checkeditinfocardlevel();">
    <table>
        <tr>
            <th colspan="2"><h4 style="color:#06F">Cập Nhật Card Level "<?php echo($arrclevel[0][1]) ?>"</h4></th>
        </tr>
        <tr>
            <th align="left" width="150px;">Tên Card Level</th>
            <td><input type="hidden" value="<?php echo($arrclevel[0][0]) ?>" name="cardlevelid" /><input class="inputtextfield" type="text" autofocus name="namecardleveledit" id="namecardleveledit" maxlength="500" onFocus="showStyle(this)" onBlur="hiddenStyle(this)" value="<?php echo($arrclevel[0][1]) ?>" /><br><font color="red"><span id="err_namecardleveledit"></span></font></td>
        </tr>
        <tr><th colspan="2"><input type="hidden" value="edit" name="acc" /><input type="submit" value="Cập Nhật" style="border-radius: 5px; padding:5px; width:100px; background:#03F; color:#FFF; font-weight:bold;" /></th></tr>
    </table>
</form>
    <?php
	}
} else {
?>
<form action="../src/control/admincardlevel.php" method="post" onSubmit="return checkaddinfocardlevel();">
    <table>
        <tr>
            <th colspan="2"><h4 style="color:#06F">Thêm Card Level</h4></th>
        </tr>
        <tr>
            <th align="left" width="150px;">Tên Card Level</th>
            <td><input class="inputtextfield" type="text" autofocus name="namecardlevel" id="namecardlevel" maxlength="500" onFocus="showStyle(this)" onBlur="hiddenStyle(this)" /><br><font color="red"><span id="err_namecardlevel"></span></font></td>
        </tr>
        <tr><th colspan="2"><input type="hidden" value="add" name="acc" /><input type="submit" value="Thêm" style="border-radius: 5px; padding:5px; width:100px; background:#03F; color:#FFF; font-weight:bold;" /></th></tr>
    </table>
</form>
<?php
}
?>
</div>
<br/>
<hr>
<br/>
<h3>Danh Sách Card Level</h3>
<div class="listbanksviewcontent">
	<table>
    	<tr>
        	<th class="stylestt title">STT</th>
        	<th class="stylenamebank title">Tên Card Level</th>
            <th class="styledatecreatebank title">Ngày C.Nhật</th>
            <th class="styleacctionbank title">Chức Năng</th>
        </tr>
        <?php
		if(count($arrcardtype) > 0) {
			$i =0;
			foreach($arrcardtype as $entry) {
				$i++;
		?>
        <tr>
       	 	<td class="nameformbank" align="center"><?php echo($i) ?></td>
        	<td class="nameformbank" style="padding-left:5px; padding-right:5px;"><?php echo($entry[1]) ?></td>
            <td class="nameformbank center"><?php echo(int_to_date($entry[2])) ?></td>
            <td class="nameformbank center"><img idct="<?php echo($entry[0]) ?>" class="class-editcardlevel-link" src="../images/icon-edit.gif" title="Chỉnh Sửa <?php echo($entry[1]) ?>" />&nbsp; | &nbsp;<img idct="<?php echo($entry[0]) ?>" class="class-deletecardlevel-link" src="../images/icon-delete.gif" title="Xóa <?php echo($entry[1]) ?>" /></td>
        </tr>
        <?php
			}
		} else {
		?>
        <tr><th colspan="4">Chưa có danh sách card level nào</th></tr>
        <?php
		}
		?>
    </table>
</div>
<script>
	$(document).on('click', '.class-editcardlevel-link', function(e) {
		elem = $(this)
		var id = elem.attr('idct');
		window.location= 'admin.php?content=quanlythetindung&&as=cardlevel&&idct=' + id;
	});
	
	$(document).on('click', '.class-deletecardlevel-link', function(e) {
		elem = $(this)
		var id = elem.attr('idct');
		if(confirm("Bạn có muốn xóa?")) {
			$.ajax({
				url: "../src/ajax/ajaxcardlevel.php",
				type: "post",
				data: {act:'del', idre: id},
				dataType:"json",
				success: function(data) {
					if(data == true) {
						elem.parent().parent().fadeOut(300, function(){elem.parent().parent().remove();});
					} else {
						alert("Cập nhật dữ liệu không thành công, hoặc mất kết nối internet");
					}
				}
			});
		}
	});
	
	function checkaddinfocardlevel() {
		var ten = $.trim($('#namecardlevel').val());
		if(ten.length == 0) {
			$('#err_namecardlevel').text('Tên Card Level không được bỏ trống');
			$('#namecardlevel').focus();
			return false;
		} else {
			$('#err_namecardlevel').text('');
		}
	}
	
	function checkeditinfocardlevel() {
		var ten = $.trim($('#namecardleveledit').val());
		if(ten.length == 0) {
			$('#err_namecardleveledit').text('Tên Card Level không được bỏ trống');
			$('#namecardleveledit').focus();
			return false;
		} else {
			$('#err_namecardleveledit').text('');
		}
	}
	
	function showStyle(field) {
		field.style.backgroundColor = '#FFFFCC';
	}
	
	function hiddenStyle(field) {
		field.style.backgroundColor = '#FFFFFF';
	}
</script>